package com.example.wishstar.view.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wishstar.MainActivity;
import com.example.wishstar.R;
import com.example.wishstar.databinding.ActivityAddMemoryBinding;
import com.example.wishstar.model.CommonDate;
import com.example.wishstar.viewmodel.AddMemoryViewModel;

public class AddMemoryActivity extends AppCompatActivity implements AddMemoryViewModel.IsRepeatCallBack{

    private String TAG = "AddMemoryActivity";
    private ActivityAddMemoryBinding activityAddMemoryBinding;
    private AddMemoryViewModel addMemoryViewModel;
    private GradientDrawable isRepeatButtonBackground, notRepeatButtonBackground;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        activityAddMemoryBinding = DataBindingUtil.setContentView(this, R.layout.activity_add_memory);
        addMemoryViewModel = new AddMemoryViewModel(this);
        activityAddMemoryBinding.setAddMemoryViewModel(addMemoryViewModel);

        addMemoryViewModel.setIsRepeatCallBack(this);

        TextView title = findViewById(R.id.post_title);
        title.setText(getString(R.string.memory_post_title));
        Button isRepeatButton = findViewById(R.id.add_memory_is_repeat_button);
        Button notRepeatButton = findViewById(R.id.add_memory_not_repeat_button);

        isRepeatButtonBackground = (GradientDrawable) isRepeatButton.getBackground();
        isRepeatButtonBackground.setColor(getColor(R.color.green_transparent));

        notRepeatButtonBackground = (GradientDrawable) notRepeatButton.getBackground();
        notRepeatButtonBackground.setColor(getColor(R.color.green_transparent));

        ImageView backImage = findViewById(R.id.image_title_back);
        backImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AddMemoryActivity.this, MainActivity.class);
                startActivity(intent);
                isRepeatButtonBackground.setColor(getColor(R.color.green_transparent));
                notRepeatButtonBackground.setColor(getColor(R.color.green_transparent));
                finish();
            }
        });

        Button button = findViewById(R.id.button_title_post);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addMemoryViewModel.post();
                //Toast.makeText(AddMemoryActivity.this, "发布成功", Toast.LENGTH_SHORT).show();
                isRepeatButtonBackground.setColor(getColor(R.color.green_transparent));
                notRepeatButtonBackground.setColor(getColor(R.color.green_transparent));
                finish();
            }
        });

        addMemoryViewModel.getDate().observe(this, new Observer<CommonDate>() {
            @Override
            public void onChanged(CommonDate commonDate) {
                activityAddMemoryBinding.setDate(commonDate);
            }
        });
    }

    @Override
    public void callback(int isRepeat, int notRepeat) {
        isRepeatButtonBackground.setColor(getColor(R.color.green_transparent));
        notRepeatButtonBackground.setColor(getColor(R.color.green_transparent));
        if (isRepeat == 1) {
            isRepeatButtonBackground.setColor(getColor(R.color.green_dark));
        }
        if (notRepeat == 1) {
            notRepeatButtonBackground.setColor(getColor(R.color.green_dark));
        }
    }

}